/*
 * monster.c
 *
 *  Created on: 28 mars 2013
 *      Author: Mouroux
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include "SDL.h"
#include "SDL_image.h"

#include "../include/monster.h"
#include "../include/misc.h"
#include "../include/game.h"


struct s_monster {
	int xmonster, ymonster;
	SDL_Surface * directed_img[4];
	enum e_way current_way;
};
t_monster monster_init() {
	t_monster monster = malloc(sizeof(*monster));
	if (!monster)
		error("Memory error");

	monster->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
	monster->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
	monster->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
	monster->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);

	monster->current_way = NORTH;

	return monster;

}

void monster_free(t_monster monster) {
	assert(monster);
	int k;
	for (k = 0; k < 4; k++)
		SDL_FreeSurface(monster->directed_img[k]);

	free(monster);
}
int monster_get_x(t_monster monster) {
	assert(monster != NULL);
	return monster->xmonster;
}

int monster_get_y(t_monster monster) {
	assert(monster != NULL);
	return monster->ymonster;
}

void monster_set_current_way(t_monster monster, enum e_way way) {
	assert(monster);
	monster->current_way = way;

}

void monster_from_map(t_monster monster, t_map map) {
	int i, j;
	for (i = 0; i < map_get_width(map); i++) {
		for (j = 0; j < map_get_height(map); j++) {
			if (map_get_cell_type(map, i, j) == CELL_PLAYER) {
				monster->xmonster = i;
				monster->ymonster = j;
			}
		}
	}
}

static int monster_move_aux(t_monster, monster, t_map map, int x1, int y1, int x2,
		int y2) {

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {
	// don't cross sceneries
	case CELL_SCENERY:
		return 0;
		break;
		// don't cross the cases
	case CELL_CASE:
		return 0;
		break;
	case CELL_PLAYER:
		break;

	default:
		break;
	}
	// Monster moved
	return 1;
}

int monster_move(t_monster monster, t_map map) {
	int xmonster = monster->xmonster;
	int ymonster = monster->ymonster;
	int move = 0;

	switch (monster->current_way) {
	case NORTH:
		if (monster_move_aux(monster, map, xmonster, ymonster - 1, xmonster, ymonster - 2)) {
			monster->ymonster--;
			move = 1;
		}
		break;

	case SOUTH:
		if (monster_move_aux(monster, map, xmonster, ymonster + 1, xmonster, ymonster + 2)) {
			monster->ymonster++;
			move = 1;
		}
		break;

	case WEST:
		if (monster_move_aux(monster, map, xmonster - 1, ymonster, xmonster - 2, ymonster)) {
			monster->xmonster--;
			move = 1;
		}
		break;

	case EAST:
		if (monster_move_aux(monster, map, xmonster + 1, ymonster, xmonster + 2, ymonster)) {
			monster->xmonster++;
			move = 1;
		}
		break;
	}

	if (move) {
		map_set_cell_type(map, xmonster, ymonster, CELL_EMPTY);
		map_set_cell_type(map, monster->xmonster,monster->ymonster, CELL_MONSTER);
	}
	return move;

}

void monster_display(t_monster monster, SDL_Surface *screen) {
	assert(monster);
	assert(monster->directed_img[monster->current_way]);
	assert(screen);

	SDL_Rect place;
	place.x = monster->x * SIZE_BLOC;
	place.y = monster->y * SIZE_BLOC;

	SDL_BlitSurface(monster->directed_img[monster->current_way], NULL, screen,
			&place);
}

